package com.mark.mall.admin.platform.service.impl;

import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.SecureUtil;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.page.PageMethod;
import com.mark.mall.admin.platform.dto.PageParamDTO;
import com.mark.mall.admin.platform.dto.UserDTO;
import com.mark.mall.admin.platform.dto.param.UserUpdateParam;
import com.mark.mall.admin.platform.entity.User;
import com.mark.mall.admin.platform.mapper.UserMapper;
import com.mark.mall.admin.platform.service.UserService;
import com.mark.mall.common.myException.MyException;
import com.mark.mall.common.resultBean.R;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * <p>
 * 管理员表 服务实现类
 * </p>
 *
 * @author admin
 * @since 2021-02-28
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Override
    public R<Boolean> login(String name, String password) {
        User sysUser = baseMapper.selectOne(
                Wrappers.<User>lambdaQuery().eq(StrUtil.isNotBlank(name), User::getName, name)
        );
        if (sysUser == null) {
            throw new MyException("名称为 【" + name + "】 的用户不存在！");
        }
        if (!StrUtil.equals(sysUser.getPassword(), SecureUtil.md5(password + sysUser.getSalt()))) {
            throw new MyException("密码错误!");
        }
        return new R<>(true, "登录成功");
    }

    @Override
    public R<Boolean> register(String userName, String password) {
        // 自定义盐值信息
        String salt = IdWorker.getIdStr();
        // 加密后的密码信息
        String md5PassWord = SecureUtil.md5(password + salt);
        User user = new User();
        user.setName(userName);
        user.setSalt(salt);
        user.setPassword(md5PassWord);
        baseMapper.insert(user);
        return new R<>(true, "注册成功");
    }

    @Override
    public R<PageInfo<UserDTO>> adminUserPageInfo(PageParamDTO param) {
        PageInfo<UserDTO> pageInfo = PageMethod.startPage(param.getPageNo(), param.getPageSize())
                .doSelectPageInfo(() -> list(null));
        return new R<>(pageInfo);
    }

    @Override
    public R<Boolean> updateUser(UserUpdateParam param) {
        User user = getById(param.getId());
        if (user == null) {
            throw new MyException("id为{" + param.getId() + "}的用户不存在！");
        }
        String newPassWord = "";
        if (!StringUtils.isEmpty(param.getPassword())) {
            // 使用md5、盐值加密
            newPassWord = SecureUtil.md5(param.getPassword() + user.getSalt());
            user.setPassword(newPassWord);
        }
        if(!StringUtils.isEmpty(param.getUserName())){
            user.setName(param.getUserName());
        }
        baseMapper.alwaysUpdateSomeColumnById(user);
        //baseMapper.updateUser(param.getUserName(), newPassWord, param.getId());
        return new R<>(true, "修改用户成功");
    }

    @Override
    public R<List<User>> findAll() {
        return new R<>(baseMapper.findAll());
    }

}
